package com.itheima.day10红黑树和map;

import java.util.HashMap;

/*4、【编号：1604】现需要将一些学生对象
（属性包含姓名和年龄），与其地址，一一对应
存储，并查看存储效果，要保证学生对象不重复，
请编写程序实现以上需求，运行效果如下：
Student{name='小红', age=20}---北京海淀
Student{name='小黄', age=22}---北京朝阳
Student{name='小白', age=25}---北京海淀
Student{name='小蓝', age=23}---北京顺义
Student{name='小绿', age=21}---北京昌平*/
public class Demo4 {
    public static void main(String[] args) {
        HashMap<Student, String> tm = new HashMap<>();
        tm.put(new Student("小红", 20), "北京海淀");
        tm.put(new Student("小黄", 22), "北京朝阳");
        tm.put(new Student("小白", 25), "北京海淀");
        tm.put(new Student("小蓝", 23), "北京顺义");
        tm.put(new Student("小绿", 21), "北京昌平");
        tm.put(new Student("小红", 20), "北京海淀");
        tm.forEach((Student stu, String s) ->
        {
            System.out.println(stu + "---" + s);
        });
    }
}

class Student {
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{name='" + this.name + "', age=" + this.age + "}";
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;

        if (age != student.age) return false;
        return name != null ? name.equals(student.name) : student.name == null;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + age;
        return result;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}